/*
 * BillEntry.java
 *
 * Created on __DATE__, __TIME__
 */

package rj.ui.medicine;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import rj.ui.utils.MedicineSelectPopup;

/**
 *
 * @author  __USER__
 */
public class PurchaseBillEntry extends javax.swing.JInternalFrame
{
	
	/** Creates new form BillEntry */
	public PurchaseBillEntry()
	{
		columnNames = new Vector<Object>(11);
		rowData = new Vector<Vector<Object>>(20);
		columnNames.add("Sr.No.");
		columnNames.add("Medicine Name");
		columnNames.add("Pack");
		columnNames.add("Units");
		columnNames.add("Free Units");
		columnNames.add("Trade rate");
		columnNames.add("Tax");
		columnNames.add("Disc");
		columnNames.add("M.R.P.");
		columnNames.add("Batch No.");
		columnNames.add("Expiry Date");
		for (int i = 0; i < 20; i++)
		{
			Vector<Object> v = new Vector<Object>();
			v.add("" + (i + 1));
			v.add(null);
			v.add(null);
			v.add(null);
			v.add(null);
			v.add(null);
			v.add(null);
			v.add(null);
			v.add(null);
			v.add(null);
			v.add(null);
			rowData.add(v);
		}
		initComponents();
		this.setName("PurchaseBillEntry");
		for (int i = 2; i < jTable1.getColumnModel().getColumnCount(); i++)
		{
			jTable1.getColumnModel().getColumn(i).setPreferredWidth(50);
		}
		jTable1.getColumnModel().getColumn(0).setPreferredWidth(10);
		jTable1.getColumnModel().getColumn(1).setPreferredWidth(240);
		
	}
	
	Vector<Object> columnNames;
	Vector<Vector<Object>> rowData;
	
	private class RituTableModel extends DefaultTableModel
	{
		
		@Override
		public Class<?> getColumnClass(int columnIndex)
		{
			if (columnIndex == 0 || columnIndex == 1 || columnIndex == 9 || columnIndex == 10) return String.class;
			return Float.class;
		}
		
		@Override
		public int getColumnCount()
		{
			return columnNames.size();
		}
		
		@Override
		public String getColumnName(int columnIndex)
		{
			return "" + columnNames.elementAt(columnIndex);
		}
		
		@Override
		public int getRowCount()
		{
			return rowData.size();
		}
		
		@Override
		public Object getValueAt(int rowIndex, int columnIndex)
		{
			return rowData.elementAt(rowIndex).elementAt(columnIndex);
		}
		
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex)
		{
			if (columnIndex == 0) return false;
			return true;
		}
		
		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex)
		{
			if (columnIndex == 10)
			{
				SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
				SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy");
				try
				{
					rowData.elementAt(rowIndex).setElementAt(sdf1.format(sdf.parse(aValue.toString())), columnIndex);
				}
				catch (ParseException e)
				{
					JOptionPane.showMessageDialog(jTable1, "Please Fill Correct Date", "Invalid Date", JOptionPane.ERROR_MESSAGE);
				}
			}
			else
			{
				rowData.elementAt(rowIndex).setElementAt(aValue, columnIndex);
			}
		}
		
	}
	
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{
		
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jButton1 = new javax.swing.JButton();
		billNoLbl = new javax.swing.JLabel();
		billNoTxt = new javax.swing.JTextField();
		billDateLbl = new javax.swing.JLabel();
		billDateTxt = new javax.swing.JTextField();
		distributerLbl = new javax.swing.JLabel();
		distributerTxt = new javax.swing.JTextField();
		
		setClosable(true);
		setIconifiable(true);
		setMaximizable(true);
		setTitle("Purchase Bill Entry");
		setToolTipText("Purchase Bill Entry");
		setDoubleBuffered(true);
		setMinimumSize(new java.awt.Dimension(1024, 768));
		try
		{
			setSelected(true);
		}
		catch (java.beans.PropertyVetoException e1)
		{
			e1.printStackTrace();
		}
		setVisible(true);
		
		jTable1.setModel(new RituTableModel());
		jTable1.setCellSelectionEnabled(true);
		jTable1.setDoubleBuffered(true);
		jTable1.addMouseListener(new java.awt.event.MouseAdapter()
		{
			public void mouseClicked(java.awt.event.MouseEvent evt)
			{
				jTable1MouseClicked(evt);
			}
		});
		jTable1.addKeyListener(new java.awt.event.KeyAdapter()
		{
			public void keyTyped(java.awt.event.KeyEvent evt)
			{
				jTable1KeyTyped(evt);
			}
		});
		jScrollPane1.setViewportView(jTable1);
		
		jButton1.setText("Add Bill");
		
		billNoLbl.setText("Bill Number");
		
		billDateLbl.setText("Bill Date");
		
		billDateTxt.addFocusListener(new java.awt.event.FocusAdapter()
		{
			public void focusLost(java.awt.event.FocusEvent evt)
			{
				billDateTxtFocusLost(evt);
			}
		});
		
		distributerLbl.setText("Distributer");
		
		distributerTxt.addFocusListener(new java.awt.event.FocusAdapter()
		{
			public void focusGained(java.awt.event.FocusEvent evt)
			{
				distributerTxtFocusGained(evt);
			}
		});
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton1,
								javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 152,
								javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING,
								javax.swing.GroupLayout.DEFAULT_SIZE, 980, Short.MAX_VALUE).addGroup(
								layout.createSequentialGroup().addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(billNoTxt,
												javax.swing.GroupLayout.Alignment.LEADING).addComponent(billNoLbl,
												javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)).addGap(
										61, 61, 61).addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(billDateTxt).addComponent(
												billDateLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)).addGap(66, 66, 66).addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(distributerTxt).addComponent(
												distributerLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)))).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addContainerGap().addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(billNoLbl).addComponent(billDateLbl).addComponent(
								distributerLbl)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(billNoTxt,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
								billDateTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(distributerTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(33, 33, 33).addComponent(
						jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 569, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1).addContainerGap(26, Short.MAX_VALUE)));
		
		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	private void distributerTxtFocusGained(java.awt.event.FocusEvent evt)
	{
		if(billDateTxt.getText().trim().isEmpty()){
			distributerTxt.transferFocusBackward();
		}
	}
	
	private void billDateTxtFocusLost(java.awt.event.FocusEvent evt)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMM-yyyy");
		try
		{
			billDateTxt.setText(sdf1.format(sdf.parse(billDateTxt.getText())));
		}
		catch (ParseException e)
		{
			JOptionPane.showMessageDialog(jTable1, "Please Fill Correct Date", "Invalid Date", JOptionPane.ERROR_MESSAGE);
			billDateTxt.setText("");
		}
	}
	
	private void jTable1MouseClicked(java.awt.event.MouseEvent evt)
	{
		if (evt.getSource() instanceof JTable)
		{
			JTable jt = (JTable)evt.getSource();
			if (jt.getSelectedColumn() == 1)
			{
				MedicineSelectPopup medicineSelectPopup = new MedicineSelectPopup(true);
				jt.setValueAt(medicineSelectPopup.seletedValue, jt.getSelectedRow(), 1);
			}
		}
	}
	
	private void jTable1KeyTyped(java.awt.event.KeyEvent evt)
	{
		if (evt.getSource() instanceof JTable)
		{
			JTable jt = (JTable)evt.getSource();
			if (jt.getSelectedColumn() == 1)
			{
				MedicineSelectPopup medicineSelectPopup = new MedicineSelectPopup(true);
				jt.setValueAt(medicineSelectPopup.seletedValue, jt.getSelectedRow(), 1);
			}
		}
	}
	
	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JLabel billDateLbl;
	private javax.swing.JTextField billDateTxt;
	private javax.swing.JLabel billNoLbl;
	private javax.swing.JTextField billNoTxt;
	private javax.swing.JLabel distributerLbl;
	private javax.swing.JTextField distributerTxt;
	private javax.swing.JButton jButton1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	// End of variables declaration//GEN-END:variables
	
}
